home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH06 / PGM6_10.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-10-10  |  2.7 KB  |  121 lines

  1. ; Conditional JMP Instructions, Part II
  2.  
  3.         .386
  4.         option    segment:use16
  5. dseg        segment    para public 'data'
  6.  
  7. Array1        word    1, 2, 3, 4, 5, 6, 7, 8
  8. Array2        word    8 dup (?)
  9.  
  10. String1        byte    "This string contains lower case characters",0
  11. String2        byte    128 dup (0)
  12.  
  13. j        sword    5
  14. k        sword    6
  15.  
  16. Result        byte    ?
  17.  
  18. dseg        ends
  19.  
  20. cseg        segment    para public 'code'
  21.         assume    cs:cseg, ds:dseg
  22.  
  23. Main        proc
  24.         mov    ax, dseg
  25.         mov    ds, ax
  26.         mov    es, ax
  27.  
  28. ; You can use the LOOP instruction to repeat a sequence of statements
  29. ; some specified number of times in an assembly language program.
  30. ; Consider the code taken from EX6_5.ASM that used the string
  31. ; instructions to produce a running product:
  32. ;
  33. ; The following code uses a loop instruction to compute:
  34. ;
  35. ;    Array2[0] := Array1[0]
  36. ;    Array2[1] := Array1[0] * Array1[1]
  37. ;    Array2[2] := Array1[0] * Array1[1] * Array1[2]
  38. ;    etc.
  39.  
  40.         cld
  41.         lea    si, Array1
  42.         lea    di, Array2
  43.         mov    dx, 1            ;Initialize for 1st time.
  44.         mov    cx, 8            ;Eight elements in the arrays.
  45.  
  46. LoopHere:    lodsw
  47.         imul    ax, dx
  48.         mov    dx, ax
  49.         stosw
  50.         loop    LoopHere
  51.  
  52.  
  53. ; The LOOPNE instruction is quite useful for controlling loops that
  54. ; stop on some condition or when the loop exceeds some number of
  55. ; iterations.  For example, suppose string1 contains a sequence of
  56. ; characters that end with a byte containing zero.  If you wanted to
  57. ; convert those characters to upper case and copy them to string2,
  58. ; you could use the following code.  Note how this code ensures that
  59. ; it does not copy more than 127 characters from string1 to string2
  60. ; since string2 only has enough storage for 127 characters (plus a
  61. ; zero terminating byte).
  62.  
  63.         lea    si, String1
  64.         lea    di, String2
  65.         mov    cx, 127            ;Max 127 chars to string2.
  66.  
  67. CopyStrLoop:    lodsb                ;Get char from string1.
  68.         cmp    al, 'a'            ;See if lower case
  69.         jb    NotLower        ;Characters are unsigned.
  70.         cmp    al, 'z'
  71.         ja    NotLower
  72.         and    al, 5Fh            ;Convert lower->upper case.
  73. NotLower:
  74.         stosb
  75.         cmp    al, 0            ;See if zero terminator.
  76.         loopne    CopyStrLoop        ;Quit if al or cx = 0.
  77.  
  78.  
  79.  
  80. ; If you do not have an 80386 (or later) CPU and you would like the
  81. ; functionality of the SETcc instructions, you can easily achieve
  82. ; the same results using code like the following:
  83. ;
  84. ; Result := J <= K;
  85.  
  86.         mov    Result, 0        ;Assume false.
  87.         mov    ax, J
  88.         cmp    ax, K
  89.         jnle    Skip1
  90.         mov    Result, 1        ;Set to 1 if J <= K.
  91. Skip1:
  92.  
  93.  
  94. ; Result := J = K;
  95.  
  96.         mov    Result, 0        ;Assume false.
  97.         mov    ax, J
  98.         cmp    ax, K
  99.         jne    Skip2
  100.         mov    Result, 1
  101. Skip2:
  102.  
  103.  
  104.  
  105.  
  106.  
  107. Quit:        mov    ah, 4ch            ;DOS opcode to quit program.
  108.         int    21h            ;Call DOS.
  109. Main        endp
  110.  
  111. cseg        ends
  112.  
  113. sseg        segment    para stack 'stack'
  114. stk        byte    1024 dup ("stack   ")
  115. sseg        ends
  116.  
  117. zzzzzzseg    segment    para public 'zzzzzz'
  118. LastBytes    byte    16 dup (?)
  119. zzzzzzseg    ends
  120.         end    Main
  121.